package mipo.top.shop.controller;

import mipo.top.base.util.JsonResult;
import mipo.top.base.util.PageUtil;
import mipo.top.shop.Dto.ConfirmDto;
import mipo.top.shop.domain.OrderPetAcquisition;
import mipo.top.shop.query.OrderPetAcquisitionQuery;
import mipo.top.shop.service.IOrderPetAcquisitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/orderPetAcquisition")
public class OrderPetAcquisitionController {

    @Autowired
    private IOrderPetAcquisitionService orderPetAcquisitionService;

    @GetMapping
    public List<OrderPetAcquisition> sad(){
        return orderPetAcquisitionService.findall();
    }

    @PutMapping
    public JsonResult su(@RequestBody OrderPetAcquisition orderPetAcquisition){
        try {
            if(orderPetAcquisition.getId()==null){
                orderPetAcquisitionService.save(orderPetAcquisition);
            }else{
                orderPetAcquisitionService.update(orderPetAcquisition);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("错误数据");
        }
    }

    @DeleteMapping("/{id}")
    public void adw(@PathVariable("id") Long id){
        orderPetAcquisitionService.remove(id);
    }

    @PostMapping
    public PageUtil<OrderPetAcquisition> asp(@RequestBody OrderPetAcquisitionQuery orderPetAcquisitionQuery){
        return orderPetAcquisitionService.queryData(orderPetAcquisitionQuery);
    }
    @PatchMapping
    private void bacthDel(@RequestBody List<Long> ids){
        orderPetAcquisitionService.patchremove(ids);
    }

    @PostMapping("/confirm")
    public JsonResult confirmOrder(@RequestBody ConfirmDto confirmDto) {
        try {
            orderPetAcquisitionService.confirmOrder(confirmDto);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("确定订单失败！"+e.getMessage());
        }

    }

    @GetMapping("/{id}")
    public JsonResult cancel(@PathVariable("id") Long id) {
        try {
            orderPetAcquisitionService.cancel(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("取消订单失败！"+e.getMessage());
        }

    }
}